package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.BatchOperationType;
import com.edu.exam.enums.curriculum.BatchPriority;
import com.edu.exam.enums.curriculum.BatchStatus;
import com.edu.exam.enums.curriculum.ExecutionMode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 批量操作任务实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：batch_operations
 *
 * 管理批量操作任务信息，记录操作类型、进度状态、执行结果等。
 * 支持大规模试题数据的批量处理。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("批量操作任务表")
@SQLDelete(sql = "UPDATE batch_operations SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class BatchOperation {

    /**
     * 批量操作唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 操作类型
     */
    @NotNull(message = "操作类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "operation_type", nullable = false, length = 20)
    private BatchOperationType operationType;

    /**
     * 操作名称
     */
    @NotBlank(message = "操作名称不能为空")
    @Size(max = 100, message = "操作名称长度不能超过100个字符")
    @Column(name = "operation_name", nullable = false, length = 100)
    private String operationName;

    /**
     * 操作人ID
     */
    @NotNull(message = "操作人ID不能为空")
    @Size(max = 50, message = "操作人ID长度不能超过50个字符")
    @Column(name = "operator_id", nullable = false, length = 50)
    private String operatorId;

    /**
     * 操作人姓名
     */
    @Size(max = 100, message = "操作人姓名长度不能超过100个字符")
    @Column(name = "operator_name", length = 100)
    private String operatorName;

    /**
     * 总项目数
     */
    @NotNull(message = "总项目数不能为空")
    @Column(name = "total_items", nullable = false)
    private Integer totalItems;

    /**
     * 已处理项目数
     */
    @Column(name = "processed_items")
    @Builder.Default
    private Integer processedItems = 0;

    /**
     * 成功项目数
     */
    @Column(name = "success_items")
    @Builder.Default
    private Integer successItems = 0;

    /**
     * 失败项目数
     */
    @Column(name = "failed_items")
    @Builder.Default
    private Integer failedItems = 0;

    /**
     * 跳过项目数
     */
    @Column(name = "skipped_items")
    @Builder.Default
    private Integer skippedItems = 0;

    /**
     * 任务状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private BatchStatus status;

    /**
     * 开始时间
     */
    @Column(name = "start_time")
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 预计持续时间（秒）
     */
    @Column(name = "estimated_duration")
    private Integer estimatedDuration;

    /**
     * 实际持续时间（秒）
     */
    @Column(name = "actual_duration")
    private Integer actualDuration;

    /**
     * 进度百分比
     */
    @Column(name = "progress_percentage", precision = 5, scale = 2)
    @Builder.Default
    private BigDecimal progressPercentage = BigDecimal.ZERO;

    /**
     * 当前处理项目
     */
    @Size(max = 100, message = "当前处理项目长度不能超过100个字符")
    @Column(name = "current_processing_item", length = 100)
    private String currentProcessingItem;

    /**
     * 错误信息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 操作数据，JSON格式
     */
    @Column(name = "operation_data", columnDefinition = "TEXT")
    private String operationData;

    /**
     * 操作参数
     */
    @Column(name = "operation_params", columnDefinition = "TEXT")
    private String operationParams;

    /**
     * 操作结果摘要
     */
    @Size(max = 1000, message = "操作结果摘要长度不能超过1000个字符")
    @Column(name = "result_summary", length = 1000)
    private String resultSummary;

    /**
     * 重试次数
     */
    @Column(name = "retry_count")
    @Builder.Default
    private Integer retryCount = 0;

    /**
     * 最大重试次数
     */
    @Column(name = "max_retry_count")
    @Builder.Default
    private Integer maxRetryCount = 3;

    /**
     * 执行优先级
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "priority", length = 20)
    private BatchPriority priority;

    /**
     * 操作模式
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "execution_mode", length = 20)
    private ExecutionMode executionMode;

    /**
     * 记录创建时间
     */
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createTime = now;
        if (this.status == null) {
            this.status = BatchStatus.PENDING;
        }
    }
}